package com.example.inoculations.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.inoculations.config.ServiceException;
import com.example.inoculations.entity.File;
import com.example.inoculations.mapper.FileMapper;
import com.example.inoculations.service.IFileService;
import com.example.inoculations.utils.PathUtils;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.internal.Constants;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements IFileService {

    @Resource
    private FileMapper fileMapper;
    @Value("${cos.secretId}")
    private String secretId;
    @Value("${cos.secretKey}")
    private String secretKey;
    @Value("${cos.bucketName}")
    private String bucketName;
    @Value("${cos.regionName}")
    private String regionName;
    @Value("${cos.prefix}")
    private String prefix;

    @Override
    public String upload(MultipartFile file) {
        // 判断文件类型
        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        String type = FileUtil.extName(originalFilename);
        long size = file.getSize();
        // 判断原始文件名
        if (originalFilename == null) {
            throw new ServiceException(Constants.NULL_VERSION_ID, "系统错误");
        }
        // 判断通过上传COS
        String filePath = PathUtils.filePathJoin(originalFilename);
        String url = uploadCOS(file, filePath);
        // 上传到腾讯云cos对象存储的同时将文件信息存储至数据库
        File saveFile = new File();
        saveFile.setName(originalFilename);
        saveFile.setType(type);
        saveFile.setSize(size / 1024);
        saveFile.setUrl(url);
        saveFile.setTime(DateUtil.parse(DateUtil.now()));
        fileMapper.insert(saveFile);
        return url;
    }

    public String uploadCOS(MultipartFile file, String filePath) {
        String key = prefix + filePath;
        try {
            // 默认不指定key的情况下，以文件内容的hash值作为文件名
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file.getInputStream(), null);
            // 使用cosClient调用第三方接口
            COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
            // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
            Region region = new Region(regionName);
            ClientConfig clientConfig = new ClientConfig(region);
            // 生成 cos 客户端。
            COSClient cosClient = new COSClient(cred, clientConfig);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //拼接返回路径
        return "https://" + bucketName + ".cos." + regionName + ".myqcloud.com/" + key;
    }
}
